package com.vim.modules.web.controller;

import com.vim.common.annotation.SysLogRecord;
import com.vim.common.model.response.Response;
import com.vim.common.model.response.RestfulResponse;
import com.vim.common.utils.ValidateUtils;
import com.vim.modules.web.model.*;
import com.vim.modules.web.service.SysMenuService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Value("${env_test}")
    private boolean envTest;

	@Resource
	private SysMenuService sysMenuService;

	/**
	 * 所有菜单
	 */
    @RequestMapping("/list")
    public RestfulResponse list(){
        List<SysMenu> list = sysMenuService.findList(new SysMenu());
        TreeList tree = new TreeList(list);
        List<SysMenu> listTree = tree.buildTree();
        return new RestfulResponse(listTree);
    }

    /**
     * 角色菜单
     */
    @RequestMapping("/xtreeList")
    public RestfulResponse xtreelist(@RequestParam(value = "roleId") String roleId){
        List<SysMenu> allMenuList = sysMenuService.findList(new SysMenu());
        List<SysMenu> roleMenuList = sysMenuService.findRoleMenuList(roleId);
        Map<String, Object> result = new XTree().build(allMenuList, roleMenuList);
        return new RestfulResponse(result);
    }

    /**
     * 编辑菜单
     */
    @SysLogRecord
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestfulResponse edit(SysMenu sysMenu){
        if(StringUtils.isNoneBlank(sysMenu.getMenuId())){
            sysMenuService.update(sysMenu);
        }else{
            sysMenuService.save(sysMenu);
        }
        return new RestfulResponse(Response.OK);
    }

    /**
     * 菜单详情
     */
    @RequestMapping("/detail")
    public RestfulResponse detail(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        return new RestfulResponse(sysMenuService.find(id));
    }

    /**
     * 删除菜单
     */
    @SysLogRecord
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public RestfulResponse del(@RequestParam(value = "id") String id){
        ValidateUtils.checkNotBlank(id);
        sysMenuService.deleteMenu(id);
        return new RestfulResponse(Response.OK);
    }

    /**
     * 编辑角色菜单
     */
    @SysLogRecord
    @RequestMapping(value = "/editRoleMenuList", method = RequestMethod.POST)
    public RestfulResponse editRoleMenuList(@RequestParam(value = "roleId") String roleId,
                                             @RequestParam(value = "menuIds", defaultValue = "") String menuIds){
        ValidateUtils.checkNotBlank(roleId);
        sysMenuService.saveRoleMenu(roleId, menuIds);
        return new RestfulResponse(Response.OK);
    }

    /**
     * 用户菜单
     */
    @RequestMapping(value = "/findUserMenuList")
    public List<UIMenu> findUserMenuList(){
        Subject subject = SecurityUtils.getSubject();
        String userId = "-1";
        if(null != subject.getPrincipal()){
            SysUser sysUser = (SysUser)subject.getPrincipal();
            userId = sysUser.getUserId();
        }
        ValidateUtils.checkNotBlank(userId);
        List<SysMenu> menuList = sysMenuService.findUserMenuList(userId);
        List<UIMenu> list = new UIMenu().build(menuList);
        return list;
    }
}

